<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/1/15
 * Time: 15:26
 */
namespace app\home\controller;
use alipay\Alipay;
use think\Db;
use wxpay\Wxpay;

class Center extends Base
{
    /**
     * 确认订单页——支付
     * 哪一类型：which（1是会员购买 2是置顶包 3是刷新包）（bag对应表的id）
     * 如果是会员：authtype（买的是何种会员(2季度  3半年  4一年）
     * 哪方支付：type（1是支付宝  2是微信）
     */
    public function alipayOrwx(){
        extract(input());
        if($type != 1 && $type != 2){die('参数错误');}
        $user_id = session('user_id');
        $number = $this->getOnlyCode();
        $today = date('Y-m-d');
        $order = array();
        $order['order_number'] = $number;
        $order['order_user_id'] = $user_id;
        $order['order_type'] = $which;
        $order['order_date'] = $today;
        $order['order_time'] = date('Y-m-d H:i:s');
        //1、买的是会员还是包
        if($which == 1){
            $money = Db::table('up_member_price')
                ->where('mp_auth',$authtype)
                ->where('mp_phone',1)
                ->value('mp_price');
            $order['order_money'] = $money;
            $order['order_auth_type'] = $authtype;
            //1-1：如果是会员算一下会员日期
            $user = Db::table('up_u_user')
                ->where('user_id', $user_id)
                ->field('user_auth,user_auth_start,user_auth_end')
                ->find();
            if($authtype == 2){$many = 3;}
            if($authtype == 3){$many = 6;}
            if($authtype == 4){$many = 12;}
            if($user['user_auth'] > 1){ //往后延
                $order['order_auth_begin'] = $user['user_auth_start'];
                $order['order_auth_end'] = $this->getNextMonthDays($user['user_auth_end'],$many+1);
            }else{//新买的和升级的都是从今天开始
                $order['order_auth_begin'] = $today;
                $order['order_auth_end'] = date("Y-m-d",strtotime("+$many month"));
            }
        }else{
            $money = Db::table('up_top_bag')
                ->where('t_b_id',$which-1)
                ->value('t_b_price');
            $order['order_money'] = $money;
        }
        Db::startTrans();
        try{
            $order_id = Db::table('up_order')->insertGetId($order);
            Db::commit();
            if($type == 2){
                $pay = new Alipay();
                if($which == 1){
                    $retrunurl = http_type ().'/home/User/member';
                }else{
                    $retrunurl = http_type ().'/home/User/recharge';
                }
                $result = $pay->pagePay ($number,'充值',$money,'',$retrunurl,http_type ().'/home/Callback/doOrder');
                return $result;
            }else{
                $wx = new Wxpay();
                $res = $wx->wxPayApi("充值",$number,$money*100,http_type ().'/home/Callback/backWx',"NATIVE",$order_id);
                return json(['status'=>200,'msg'=>$res,'oid'=>$order_id]);
            }
        }catch (\Exception $e) {
            Db::rollback();
            return json(['status'=>0,'msg'=>'生成订单失败，请联系管理员']);
        }
    }

    /**
     * 生成唯一订单号
     * @return string
     * @throws \think\Exception
     */
    public function getOnlyCode(){
        $code = date('YmdHis').GetRandCode(7);
        $repet = Db::name('order')->where('order_number',$code)->count();
        if(!$repet){
            return $code;
        }else{
            $this->getOnlyCode();
        }
    }
    /**
     * 获取某个日期几个月后的这天
     * @param $date
     * @param $between
     * @return false|string
     */
    public function getNextMonthDays($date,$between){
        $firstday = date('Y-m-01', strtotime($date));
        $lastday = strtotime("$firstday +$between month -1 day");
        $day_lastday = date('d', $lastday); //获取下个月份的最后一天
        $day_benlastday = date('d', strtotime("$firstday +1 month -1 day")); //获取本月份的最后一天

        //获取当天日期
        $Same_day = date('d', strtotime($date));
        //判断当天是否是最后一天   或 下月最后一天 等于 本月的最后一天
        if($Same_day == $day_benlastday ||$day_lastday == $Same_day){
            $day = $day_lastday;

        }else{
            $day = $Same_day;

        }
        $day = date('Y',$lastday).'-'.date('m',$lastday).'-'.$day;

        return $day;

    }

}